package com.kzw.oa.comm.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.core.util.BeanUtil;
import com.kzw.core.util.ContextUtil;
import com.kzw.core.util.ExtjsUtil;
import com.kzw.core.util.StringUtil;
import com.kzw.core.web.action.BaseAction;
import com.kzw.oa.comm.model.TongBao;
import com.kzw.oa.comm.service.LogService;
import com.kzw.oa.comm.service.TongBaoService;
import com.kzw.oa.comm.type.LogType;
import com.kzw.oa.model.system.FileAttach;
import com.kzw.oa.service.system.FileAttachService;
import com.kzw.oa.util.JSON;

/**
 * @author kzwang 2016-07-30 08:52
 */
@Controller
@Scope("prototype")
public class TongBaoAction extends BaseAction {
	@Autowired
	private TongBaoService tongBaoService;
	@Autowired
	private FileAttachService attachService;
	private TongBao tongBao;
	private Long oid;
	
	@Autowired
	private LogService logService;
	
	/**
	 * 显示我单位能显示的列表
	 */
	public String listMy(){
		HttpServletRequest request = getRequest();
		PageRequest pageRequest = ExtjsUtil.getPageReq(request);
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		
		List<TongBao> list = tongBaoService.listMy(filters);
		// 构造分页信息
		Page<TongBao> page = ExtjsUtil.toPageForExtJs(pageRequest, list);
		
		jsonString = new JSON(page).buildWithFilters(3);		
		return SUCCESS;
	}
	
	/**
	 * 桌面显示：我单位能显示的列表
	 */
	public String desktop(){
		
		HttpServletRequest request = getRequest();
		PageRequest pageRequest = ExtjsUtil.getPageReq(request);
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		
		List<TongBao> list = tongBaoService.listMy(filters);
		Page<TongBao> page = ExtjsUtil.toPageForExtJs(pageRequest, list);
		
		request.setAttribute("page", page);
		return "home";
	}
	

	/**
	 * 显示列表
	 */
	public String list() {
		logService.log(LogType.Query, getRequest(), "案件报备->通报管理");
		
		HttpServletRequest request = getRequest();
		PageRequest pageRequest = ExtjsUtil.getPageReq(request);
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		Long depId = ContextUtil.getCurrentDept().getDepId();
		filters.add(new StringPropertyFilter("EQL_dept.depId", depId+""));
		
		Page<TongBao> page = tongBaoService.search2(pageRequest, filters);

		jsonString = new JSON(page).buildWithFilters(3);
		return SUCCESS;
	}

	/**
	 * 批量删除
	 */
	public String multiDel() {
		logService.log(LogType.Delete, getRequest(), "案件报备->通报管理");
		
		String[] ids = getRequest().getParameterValues("ids");
		tongBaoService.remove(ids);
		jsonString = "{success:true}";
		return SUCCESS;
	}

	/**
	 * 显示详细信息
	 */
	public String get() {
		TongBao tongBao = tongBaoService.get(oid);
		setJsonString(new JSON(tongBao).buildWithFilters(2));
		return SUCCESS;
	}
	
	public String detail() {
		HttpServletRequest request = getRequest();
		String opt = request.getParameter("opt");
		TongBao tongBao = tongBaoService.get(oid);
		
		List<StringPropertyFilter> filters = new ArrayList<StringPropertyFilter>();
		List<TongBao> list = tongBaoService.listMy(filters);
		int index = 0;
		for(TongBao tb : list) {
			if(tb.getOid().equals(oid)) {
				break;
			}
			index++;
		}
		
		request.setAttribute("havePreFlag", "xx");
		request.setAttribute("haveNextFlag", "xx");
		if(index == 0) {
			request.setAttribute("havePreFlag", "endPre");
		}
		if(index == list.size() - 1) {
			request.setAttribute("haveNextFlag", "endNext");
		}
		
		if(StringUtils.isNotBlank(opt)) {
			if(opt.equals("_pre")) {
				tongBao = list.get(index - 1);
			} else if(opt.equals("_next")) {
				tongBao = list.get(index + 1);
			}
		}
		request.setAttribute("tongbao", tongBao);
		
		// 附件
		String fileIds = tongBao.getFileIds();
		if (StringUtils.isNotEmpty(fileIds)) {
			fileIds = fileIds.substring(0, fileIds.length() - 1);
			List<FileAttach> attachs = attachService
					.getByHql("from FileAttach where id in (" + fileIds + ")");
			getRequest().setAttribute("attachs", attachs);
		}
		return "detail";
	}
	

	/**
	 * 添加及保存操作
	 */
	public String save() {
		String content = tongBao.getContent().replace("\n", "<br>");
		tongBao.setContent(content);
		
		if (tongBao.getOid() == null) {
			tongBao.setAuthor(ContextUtil.getCurrentUser());
			tongBao.setDept(ContextUtil.getCurrentDept());
			tongBaoService.saveOrUpdate(tongBao);
		} else {
			TongBao orgTongBao = tongBaoService.get(tongBao.getOid());
			try {
				BeanUtil.copyNotNullProperties(orgTongBao, tongBao);
				tongBaoService.saveOrUpdate(orgTongBao);
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}
		setJsonString("{success:true}");
		return SUCCESS;
	}

	
	/**
	 * 导出选中记录数
	 * */
	public String exportSel() {
		HttpServletRequest request = getRequest();
		if (ExtjsUtil.isExport(request)) {
			String sids = request.getParameter("sids");
			List<TongBao> result = tongBaoService.getByIds(StringUtil
					.idStrToArr(sids, Long.class));
			request.setAttribute("__exportList", result);
		}

		return SUCCESS;
	}

	public Long getOid() {
		return oid;
	}

	public void setOid(Long oid) {
		this.oid = oid;
	}

	public TongBao getTongBao() {
		return tongBao;
	}

	public void setTongBao(TongBao tongBao) {
		this.tongBao = tongBao;
	}

}
